<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/11/23
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;

class Count extends Base
{
    public function index()
    {
        $type = Db::name('attr')->where('type','cash')->order('sort desc,id desc')->select();
        $this->assign('type', json_encode($type));
        return $this->fetch();
    }

    public function ajax_get_count_data()
    {
        $time1 = input('start');
        $time2 = input('end');
        $start = $end = null;
        if ($time1 && $time2) {
            $start = strtotime($time1);
            $end = strtotime($time2) + 24 * 3600 - 1;
        }
        $type = input('type_id/d') ?: 0;
        if ($type)
            $where['cash_type_id'] = ['eq', $type];

        $where['time'] = ['between', [$start, $end]];

        $data1 = Db::name('cash')->where('type',1)->where($where)->field('sum(price) as nums')->find();
        $data2 = Db::name('cash')->where('type', 2)->where($where)->field('sum(price) as nums')->find();
        $data = [
            'income' => $data1['nums']?:0,
            'out' => $data2['nums']?:0,
            'total' => $data1['nums'] - $data2['nums'],
        ];
        return jsonReturn(1,'',$data);
    }
}